package com.saifan.wyy_ov.ui.property;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.QuestionnaireInfoBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.NoScrollListView;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-6-7 14:53:45
 * @descrption 社区调查
 */

public class QuestionnaireInfoActivity extends BaseActivity {
    private Toolbar toolbar;
    private TextView WJDCBT;
    private TextView WJDCDCLR;
    private TextView WJDCCJSJ;
    private TextView WJDCKSSJ;
    private TextView WJDCDCSJ;
    private NoScrollListView listView;

    CommonAdapter<QuestionnaireInfoBean.Options> adapter;
    QuestionnaireInfoBean questionnaireInfoBean;
    List<QuestionnaireInfoBean.Options> options;
    int mPostion=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        questionnaireInfoBean = new QuestionnaireInfoBean();
        options = questionnaireInfoBean.getOptions();
        initAdapter();
        query();
    }


    private void initAdapter() {
        adapter = new CommonAdapter<QuestionnaireInfoBean.Options>(this, options, R.layout.options_item) {
            @Override
            public void convert(ViewHolderHelper helper, QuestionnaireInfoBean.Options item) {
                helper.setText(R.id.DCXX_MC, item.getDCXX_MC());
                helper.setText(R.id.DCXX_CSTPS, item.getDCXX_CSTPS()+item.getDCXX_TPS() + "票");
                RadioButton radioButton = helper.getView(R.id.radioButton1);
                radioButton.setChecked(item.isCheck());


            }
        };
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (options.get(position).isCheck()) {
                    options.get(position).setIsCheck(false);
                    mPostion=-1;
                } else {
                    mPostion=position;
                    options.get(position).setIsCheck(true);
                    for (int i = 0; i < options.size(); i++) {
                        if (i != position) {
                            options.get(i).setIsCheck(false);
                        }
                    }
                }
                adapter.mDatas = options;
                adapter.notifyDataSetChanged();


            }
        });
    }

    public void setContentView() {
        setContentView(R.layout.activity_questionnaire_info);
        this.listView = (NoScrollListView) findViewById(R.id.listView);
        this.WJDCDCSJ = (TextView) findViewById(R.id.WJDC_DCSJ);
        this.WJDCKSSJ = (TextView) findViewById(R.id.WJDC_KSSJ);
        this.WJDCCJSJ = (TextView) findViewById(R.id.WJDC_CJSJ);
        this.WJDCDCLR = (TextView) findViewById(R.id.WJDC_DCLR);
        this.WJDCBT = (TextView) findViewById(R.id.WJDC_BT);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void query() {

        GetData getData = new GetData();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("XMBS", getRoomInfoBean().getXMBS());
            jsonObject.put("CYRYB_KHWJ", getRoomInfoBean().getWYKHDA_ZJ());


        } catch (JSONException e) {
            e.printStackTrace();
        }


        getData.getDataJson(this, Api.Questionnaire.QUESTIONNAIRE_INFO, jsonObject, getString(R.string.wait), new ResponseData() {
            @Override
            public void onSuccess(String json) {
                List<QuestionnaireInfoBean> list = new Gson().fromJson(json, new TypeToken<List<QuestionnaireInfoBean>>() {
                }.getType());
                if (!list.isEmpty()) {
                    questionnaireInfoBean = list.get(0);
                    WJDCDCSJ.setText(StringUtil.returnShow(questionnaireInfoBean.getWJDC_DCSJ()));
                    WJDCKSSJ.setText(StringUtil.returnShow(questionnaireInfoBean.getWJDC_KSSJ()));
                    WJDCCJSJ.setText(StringUtil.returnShow(questionnaireInfoBean.getWJDC_CJSJ()));
                    WJDCDCLR.setText(StringUtil.returnShow(questionnaireInfoBean.getWJDC_DCLR()));
                    WJDCBT.setText(StringUtil.returnShow(questionnaireInfoBean.getWJDC_BT()));

                    options = questionnaireInfoBean.getOptions();
                    adapter.mDatas = options;
                    adapter.notifyDataSetChanged();


                }


            }

            @Override
            public void onFailed(String str) {

            }
        });
    }

    public void submit(View view) {

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(getString(R.string.yyyy_mm_dd));
            Date begin= dateFormat.parse(questionnaireInfoBean.getWJDC_KSSJ().substring(0, 10));
            boolean isbegin=  new Date().before(begin);
            if (isbegin){
                ToastUtil.showToast(this,"还没开始！");
                return;
            }



        }catch (Exception e){

        }

        try {

            SimpleDateFormat dateFormat = new SimpleDateFormat(getString(R.string.yyyy_mm_dd));
            Date end=dateFormat.parse(questionnaireInfoBean.getWJDC_DCSJ().substring(0, 10));

            boolean isend=  new Date().after(end);
            if (isend){
                ToastUtil.showToast(this,"调查已结束！");
                return;
            }

        }catch (Exception e){

        }

        if (questionnaireInfoBean.isSFCY()){
            ToastUtil.showToast(this,"您已经提交过了！");
            return;
        }
        if (mPostion==-1){
            ToastUtil.showToast(this,"请选择您的意见！");
            return;
        }


        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("DCXX_ZJ", options.get(mPostion).getDCXX_ZJ());
            jsonObject.put("CYRYB_KHWJ", getRoomInfoBean().getWYKHDA_ZJ());
            jsonObject.put("WJDC_ZJ", questionnaireInfoBean.getWJDC_ZJ());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        GetData getData = new GetData();

        getData.getDataJson(this, Api.Questionnaire.QUESTIONNAIRE_SUBMIT, jsonObject, getString(R.string.wait), new ResponseData() {
            @Override
            public void onSuccess(String json) {
                ToastUtil.showToast(QuestionnaireInfoActivity.this, "提交成功，谢谢您的宝贵意见");

            }

            @Override
            public void onFailed(String str) {

            }
        });

    }

}
